package cn.wolfcode.web.controller;


import cn.wolfcode.common.exception.BusinessException;
import cn.wolfcode.common.web.Result;
import cn.wolfcode.domain.OrderInfo;
import cn.wolfcode.service.IOrderInfoService;
import cn.wolfcode.web.feign.IPayFeignApi;
import cn.wolfcode.web.msg.SeckillCodeMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * Created by lanxw
 */
@RestController
@RequestMapping("/orderPay")
@RefreshScope
public class OrderPayController {
    @Autowired
    private IOrderInfoService orderInfoService;
    @Autowired
    private IPayFeignApi payFeignApi;

    /**
     * 付款
     *
     * @param orderNo
     * @param type
     * @return
     */
    @GetMapping("/pay")
    public Result<String> pay(String orderNo,Integer type) {
        if (OrderInfo.PAYTYPE_ONLINE.equals(type)) {
            // 在线支付
            String html = orderInfoService.payOnline(orderNo);
            return Result.success(html);
        } else {
            // 积分支付
            orderInfoService.payIntegral(orderNo);
        }
        return Result.success();
    }

    /**
     * 异步回调，订单的支付之后的业务逻辑处理
     *
     * @param params 支付宝回调时候参数需要的封装类型,支付宝POST过来的反馈信息
     * @return
     */
    @RequestMapping("/notify_url")
    public String notifyUrl(@RequestParam Map<String,String> params) {
        // 进行验证签名的操作，远程调用
        Result<Boolean> result = payFeignApi.rsaCheckV1(params);
        if (result == null || result.hasError()) {
            throw new BusinessException(SeckillCodeMsg.PAY_SERVER_ERROR);
        }
        // 到这里订单已经付款完了，那么需要进行订单的状态进行修改
        // 获取订单号
        String orderNo = params.get("out_trade_no");
        orderInfoService.paySuccess(orderNo);
        return "success";
    }

    @Value("${pay.frontEndPayUrl}")
    private String frontEndPayUrl;
    @Value("${pay.errorUrl}")
    private String errorUrl;

    /**
     * 同步回调地址，订单付款完成功之后展示的页面
     *
     * @param params
     * @param response
     * @throws IOException
     */
    @RequestMapping("/return_url")
    public void returnUrl(@RequestParam Map<String,String> params,HttpServletResponse response) throws IOException {
        // 进行验证签名的操作，远程调用
        Result<Boolean> result = payFeignApi.rsaCheckV1(params);
        if (result == null || result.hasError()) {
            response.sendRedirect(errorUrl);
            return;
        }
        String orderNo = params.get("out_trade_no");
        System.out.println(frontEndPayUrl+orderNo);
        response.sendRedirect(frontEndPayUrl+orderNo);
    }

    @RequestMapping("/refund")
    public Result<String> refund(String orderNo) {
        OrderInfo orderInfo = orderInfoService.find(orderNo);
        // 退款
        if (OrderInfo.PAYTYPE_ONLINE.equals(orderInfo.getPayType())) {
            // 进行支付宝的退款
            orderInfoService.refundOnline(orderInfo);
        } else {
            // 进行积分的退款
            orderInfoService.refundIntegral(orderInfo);
        }
        return Result.success();
    }
}
